package com.jsu.springboot_crop.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jsu.springboot_crop.dao.BankDao;
import com.jsu.springboot_crop.entity.Bank;
import com.jsu.springboot_crop.entity.Finance;
import com.jsu.springboot_crop.entity.FinanceUserDetails;
import com.jsu.springboot_crop.entity.Recommend;
import com.jsu.springboot_crop.service.BankService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author wang
 * @Date 2023/8/26 15:29
 */
@Service
public class BankServiceImpl implements BankService {
    @Resource
    BankDao bankDao;

    Integer pageSize=10;

    @Override
    public List<Bank> selectAllBank() {
        List<Bank> banks = bankDao.selectAllBank();
        return banks;
    }

    @Override
    public PageInfo<Recommend> selectRecommend(Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        List<Recommend> recommenders = bankDao.selectRecommendFromOrder();
        PageInfo<Recommend> recommenderPageInfo=new PageInfo<>(recommenders);
        return recommenderPageInfo;
    }

    @Override
    public PageInfo<Finance> selectByFinance(Finance finance,Integer pageNum) {
        PageHelper.startPage(pageNum,20);
        List<Finance> finances = bankDao.selectByFinance(finance);
        for(Finance f: finances){
            String combinationName = "";
            if(f.getCombinationName1()==null&&f.getCombinationName2()==null){
                combinationName += "无";
            }
            if(f.getCombinationName1()!=null){
                combinationName += f.getCombinationName1();
            }
            if(f.getCombinationName2()!=null){
                combinationName += "、" + f.getCombinationName2();
            }
            f.setCombinationName(combinationName);
        }
        PageInfo<Finance> financePageInfo = new PageInfo<>(finances);
        return financePageInfo;
    }

    @Override
    public List<Finance> selectByBankId(Integer bankId) {
        return bankDao.selectByBankId(bankId);
    }

    @Override
    public List<Finance> selectByIdName(Integer bankId, String name) {
        return bankDao.selectByIdName(bankId,name);
    }

    @Override
    public Finance selectFinanceById(Integer financeId) {
        return bankDao.selectFinanceById(financeId);
    }

    @Override
    public void updateByFinance(Finance finance) {
        finance.setUpdateTime(new Date());
        bankDao.updateByFinance(finance);
    }

    @Override
    public FinanceUserDetails selectFinanceUser(String name, Integer bankId) {
        FinanceUserDetails user = new FinanceUserDetails();
        Bank bank = bankDao.selectBankById(bankId);
        user.setRate(bank.getRate());
        user.setBankPhone(bank.getBankPhone());
        user.setIntroduce(bank.getIntroduce());
        user.setBankName(bank.getBankName());
        System.out.println("user:"+ user);
        return user;
    }

    @Override
    public List<String> getPic(Integer id) {
        System.out.println("融资人："+id);
        String pic = bankDao.getPic(id);

        String[] strings = pic.split(" ");
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < strings.length; i++) {
            list.add(strings[i]);
        }
        System.out.println(list);
        return list;
    }

    @Override
    public String selectBankName(Integer bankid) {
        return bankDao.selectBankName(bankid);
    }
}
